<template>
  <div class="app-container">

    <div>
      <el-input v-model="pageDto.where.name" placeholder="请输入行业名称" style="width: 250px;margin-left: 20px;"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="pageDto.now = 1;search()">搜索</el-button>
      <el-button type="danger" icon="el-icon-refresh" @click="reset">清空</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-right: 20px;float: right;"
        @click="dialogFormVisible = true; deviceType = {}; add()">添加</el-button>
    </div>

    <!-- 页面表格渲染 -->
    <div style="margin-top: 20px;">
      <el-table :data="industryList" style="width: 100%;margin-bottom: 20px;margin-bottom: 20px;"
        :header-cell-style="{ background: '#F7F7F8', color: '#18191B', 'text-align': 'center' }">
        <el-table-column prop="name" label="行业名称" align="center"> </el-table-column>
        <el-table-column prop="createTime" label="创建日期" :formatter="dateFormat" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link @click="edit(scope.row)" type="primary" :underline="false">编辑</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageDto.now"
        :page-sizes="[5, 10, 50, 100]" :page-size="pageDto.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total" style="text-align: center;">
      </el-pagination>
    </div>

    <el-dialog :title="industry.id?'编辑行业信息':'添加行业信息'" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="industry" :rules="rules" ref="industry" label-width="150px" class="demo-ruleForm" size="mini">
        <el-form-item label="行业名称" prop="name">
          <el-input v-model="industry.name" placeholder="行业名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('industry')">重置</el-button>
        <el-button @click="dialogFormVisible = false; resetForm('industry')">取 消</el-button>
        <el-button type="primary" @click="submitForm('industry')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { page, add } from "@/api/industry"
export default {
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入行业名称', trigger: 'blur' },
        ],
      },
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      pages: 0,
      total: 0,
      dialogFormVisible: false,
      industryList: [],
      industry: {},
      
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.industry).then((res) => {
            if (res.code == 0) {
              this.dialogFormVisible = false;
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.industry = {}
              this.search()
            } else {
              this.$message.error(res.msg);
            }
          })
        } else {
          this.$message.error('关键信息未填写！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    search() {
      page(this.pageDto).then((res) => {
        this.industryList = res.list;
        this.pages = res.pages;
        this.total = res.total;
      });
    },

    handleSizeChange(size) {
      this.pageDto.size = size;
      this.search();
    },

    handleCurrentChange(now) {
      this.pageDto.now = now;
      this.search();
    },

    //格式日期
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc) {
        if (daterc.indexOf('T') === -1) return daterc
        const arr = daterc.split('T')
        const d = arr[0]
        const darr = d.split('-')
        const t = arr[1]
        const tarr = t.split('.000')
        const marr = tarr[0].split(':')
        const dd = darr[0] + '-' + darr[1] + '-' + darr[2] + ' ' + parseInt(marr[0]) + ':' + marr[1] + ':' + marr[2].substring(0, 2)
        return dd
      }
    },

    reset() {
      this.pageDto.where = {}
    },

    edit(data){
      this.industry = data
      this.dialogFormVisible = true
    }

  },
  created() {
    this.search()
  }
}
</script>

<style></style>
